package ext.acae2.mail;

import java.util.List;
import java.util.Map;

import wt.mail.EMailMessage;
import wt.org.WTPrincipal;
import wt.session.SessionHelper;

/**
 * @desription WC Email send util class
 * @author cnie
 * @date 2013-6-24 下午3:27:32
 */
public class EmailUtility {
	
	
	public static final String EMAIL_TITLE="EMAIL_TITLE";
	public static final String EMAIL_CONTENT="EMAIL_CONTENT";
	public static final String EAMIL_ATTACHEMENT="EAMIL_ATTACHEMENT";

	/**
	 * 
	 * @description send email
	 * @author cnie
	 * @date 2013-6-24 下午3:33:12
	 * @version 1.0
	 * @param map  email 的内容（主题，内容等）
	 * @param toPrincipals
	 * @param ccprincipals
	 */
	public static void sendEmail(Map<String, Object> map,
			List<WTPrincipal> toPrincipals, List<WTPrincipal> ccprincipals) {
		String title = (String) map.get(EMAIL_TITLE);
		String content = (String)map.get(EMAIL_CONTENT);
		try{
			EMailMessage email = EMailMessage.newEMailMessage();
			
			email.setSubject(title);
			email.addPart(content, "text/html");
			
			email.setOriginator(SessionHelper.manager.getAdministrator());
			if(toPrincipals!=null){
				for(WTPrincipal toUser:toPrincipals){
					email.addRecipient(toUser, EMailMessage.TO_RECIPIENT);
				}
			}
			if(ccprincipals!=null){
				for(WTPrincipal ccUser:ccprincipals){
					email.addRecipient(ccUser, EMailMessage.CC_RECIPIENT);
				}
			}
			
			email.send(false);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
